package cn.sunlelin.educationserver.service.Impl;

import cn.sunlelin.educationserver.entity.Lesson;
import cn.sunlelin.educationserver.entity.Lessonsituation;
import cn.sunlelin.educationserver.mapper.LessonMapper;
import cn.sunlelin.educationserver.mapper.LessonsituationMapper;
import cn.sunlelin.educationserver.service.LessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class LessonServiceImpl implements LessonService {
    @Autowired
    private LessonMapper lessonMapper;

    @Autowired
    private LessonsituationMapper lessonsituationMapper;
    @Override
    public Boolean createLesson(Lesson lesson) {
        lesson.setCreateTime(new Date());
        lessonMapper.createLesson(lesson);
        return true;
    }

    @Override
    public Boolean updateLesson(Lesson lesson) {
        if(this.selectLessonById(lesson.getLessonId())==null)
            return false;
        lesson.setUpdateTime(new Date());
        lessonMapper.updateLesson(lesson);
        return true;
    }
    @Override
    public Lesson selectLessonById(Integer lessonId) {
        return lessonMapper.selectLessonById(lessonId);
    }
    @Override
    public List<Map<String,Object>> seleteLessonByCourseId(Integer courseId, Integer userId){
        List<Map<String,Object>> lessonList=lessonMapper.seleteLessonByCourseId(courseId);
        Lessonsituation lessonsituation=new Lessonsituation();
        lessonsituation.setUserId(userId);
        for(int i=0;i<lessonList.size();i++){
            Map<String,Object> map=lessonList.get(i);
            lessonsituation.setLessonId((Integer) map.get("lessonId"));
            if(lessonsituationMapper.selectLessonsituationByUserAndLesson(lessonsituation)!=null)
                map.put("complete",true);
            else
                map.put("complete",null);
            lessonList.set(i,map);
            System.out.println(lessonList.get(i));
        }
        return lessonList;
    }

    @Override
    public List<Map<String,Object>> seleteLessonByCourseIdUpdateTimeDesc(Integer courseId){
        return lessonMapper.seleteLessonByCourseIdUpdateTimeDesc(courseId);
    }
    @Override
    public Boolean deleteLesson(Integer lessonId) {
        if(this.selectLessonById(lessonId)==null)
            return false;
        Lesson lesson=new Lesson();
        lesson.setLessonId(lessonId);
        lesson.setUpdateTime(new Date());
        lessonMapper.deleteLesson(lesson);
        return true;
    }
}
